package com.jtech.together.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * 通用工具类
 * Created by wuxubaiyang on 2017/5/11.
 */

public class Util {
    /**
     * 根据生日获取年龄
     *
     * @param birthday
     * @return
     */
    public static int getAge(long birthday) {
        Calendar born = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        born.setTimeInMillis(birthday);
        //如果生日在当前时间之后则返回0
        if (born.after(now)) {
            return 0;
        }
        //计算年差
        return now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
    }

    /**
     * 判断生日时间是否小于当前时间
     *
     * @return
     */
    public static boolean isBornFuture(long birthday) {
        Calendar born = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        born.setTimeInMillis(birthday);
        return born.after(now);
    }

    /**
     * 格式化生日
     *
     * @param timeTemp
     * @return
     */
    public static String formatBirthday(long timeTemp) {
        return new SimpleDateFormat("yyyy-MM-dd")
                .format(timeTemp);
    }

    /**
     * 格式化消息时间
     *
     * @param timeTemp
     * @return
     */
    public static String formatMessageDate(long timeTemp) {
        return new SimpleDateFormat("MM-dd hh:mm")
                .format(timeTemp);
    }

    /**
     * 格式化聊天消息时间戳
     *
     * @param timeTemp
     * @return
     */
    public static String formatChatMessageDate(long timeTemp) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(timeTemp);
    }

    /**
     * 格式化媒体的长度
     *
     * @param timeMillis
     * @return
     */
    public static String formatMediaLength(long timeMillis) {
        if (timeMillis > 0) {
            long h = timeMillis / (60 * 60 * 1000);
            long m = timeMillis / (60 * 1000) - h * 60;
            long s = timeMillis / 1000 - h * 60 * 60 - m * 60;
            return parseNumber(h) + ":" + parseNumber(m) + ":" + parseNumber(s);
        }
        return "00:00:00";
    }

    /**
     * 格式化数量
     *
     * @param count
     * @return
     */
    public static String formatCount(int count) {
        if (count < 10000) {
            return count + "";
        } else {
            DecimalFormat decimalFormat = new DecimalFormat("#0.0");
            return decimalFormat.format(count * 1.0 / 10000) + "万";
        }
    }

    /**
     * 处理数字是否为两位数，不足的补齐
     *
     * @param number
     * @return
     */
    private static String parseNumber(long number) {
        return (number >= 10 ? "" : "0") + number;
    }

    /**
     * 格式化double类型数据，保留小数点后两位
     *
     * @param d
     * @return
     */
    public static String formatDouble(double d) {
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        return decimalFormat.format(d);
    }

    /**
     * 获取版本名
     *
     * @return 当前应用的版本号
     */
    public static String getVersion(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "未知";
    }

    /**
     * 获取版本号
     *
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 应用打分
     *
     * @param context
     */
    public static void appMark(Context context) {
        Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 文本分享
     *
     * @param context
     * @param text
     */
    public static void shareText(Context context, String text) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, text);
        shareIntent.setType("text/plain");
        context.startActivity(Intent.createChooser(shareIntent, "分享到"));
    }

    /**
     * 图片分享
     *
     * @param context
     * @param uri
     */
    public static void shareSingleImage(Context context, Uri uri) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.setType("image/*");
        context.startActivity(Intent.createChooser(shareIntent, "分享到"));
    }

    /**
     * 分享多张图片
     *
     * @param context
     * @param uriList
     */
    public static void shareMultipleImage(Context context, ArrayList<Uri> uriList) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
        shareIntent.setType("image/*");
        context.startActivity(Intent.createChooser(shareIntent, "分享到"));
    }

    /**
     * 保存bitmap到本地
     *
     * @param file
     * @param bitmap
     */
    public static void saveBitmapAsJpeg(File file, Bitmap bitmap) {
        FileOutputStream fileOutputStream = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fileOutputStream) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}